import Vue from 'vue';
import App from './App.vue';
import router from './router';
import store from './store';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import EventBus from '@/event-bus.js';


Vue.use(ElementUI);
Vue.config.productionTip = false;

// 确保 Vue 实例只创建一次
export default new Vue({
  router,
  store,
  render: (h) => h(App),
  mounted() {
    // 将 EventBus 的监听器添加到 mounted 钩子中
    EventBus.$on('my-event', this.handleMyEvent);
  },
  methods: {
    handleMyEvent(data) {
      // 处理事件
      console.log('Event received:', data);
    },
    emitMyEvent(data) {
      // 从组件内部触发事件
      EventBus.$emit('my-event', data);
    }
  },
  beforeDestroy() {
    // 确保在组件销毁前移除事件监听器
    EventBus.$off('my-event', this.handleMyEvent);
  }
}).$mount('#app');